package com.ag.wzp.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.bean.TbEventmackup;
import com.ag.bean.TbMackupAnnex;
import com.ag.bean.TbUri;
import com.ag.common.AnnexPath;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.lzw.dao.LzwTbUriDAO;
import com.ag.wzp.dao.WzpTbEventMackupDAO;
import com.ag.wzp.dao.WzpTbMackupAnnexDAO;

@Service("mackupServiceImpl")
public class MackupServiceImpl {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(MackupServiceImpl.class);
	@Resource(name = "iDServiceImpl")
	private IIDService idServiceImpl;
	@Resource(name = "wzpTbEventMackupDAO")
	WzpTbEventMackupDAO eventmackeupDAO;
	@Resource(name = "wzpTbMackupAnnexDAO")
	WzpTbMackupAnnexDAO mackupAnnexDAO;
	@Resource(name="lzwTbUriDAO")
	private LzwTbUriDAO uriDAO;
	
	public boolean save(TbEventmackup mackup,String annexAddr,String annexName) {
		try {
			mackup.setEmId(idServiceImpl.findTablePK(IDUtil.TB_EVENT_MACKUP));
			mackup.setEmTime(new Date());
			mackup.setA(IDUtil.PRESERVE);
			this.eventmackeupDAO.save(mackup);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		logger.info(annexAddr);
		logger.info(annexName);
		try {
			if (annexAddr != "" && annexName != "") {
				logger.info("do sava");
				String[] addrs = annexAddr.split(",");
				String[] names = annexName.split(",");
				if (addrs.length == names.length) {
					//TbUri url =  uriDAO.findById(AnnexPath.CASE_ANNEX_PATH);
//					String savePath ;
//					if(url!=null && url.getUPath()!=null){
//						savePath = url.getUPath();
//					} else {
//						return false;
//					}
					for (int i = 0; i < addrs.length; i++) {
						TbMackupAnnex ma = new TbMackupAnnex();
						ma.setMaId(idServiceImpl.findTablePK(IDUtil.TB_MACKUP_ANNEX));
						ma.setTbEventmackup(mackup);
						ma.setMaAddress(mackup.getTbEvent().getEId()+"/"+addrs[i]);
						ma.setMaName(names[i]);
						ma.setA(IDUtil.PRESERVE);
						this.mackupAnnexDAO.save(ma);
					}
				} else {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public List<TbEventmackup> findByEId(String EId){
		return this.eventmackeupDAO.findByEId(EId);
	}

	public List<TbMackupAnnex> findAnnexByEId(String eId) {
		return this.mackupAnnexDAO.findAnnexByEId(eId);
	}
}
